#include "widget.h"
#include "ui_widget.h"
#include "recvbox.h"
#include "sendbox.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->scrollLayoutWidget->layout()->setSpacing(25);                               // 设置垂直布局里控件间的距离为25像素

    // 获取QScrollArea的垂直滚动条
    // QScrollBar *verticalScrollBar = ui->scrollArea->verticalScrollBar();


    connect(ui->pushButton,&QPushButton::clicked,this,[=]{

        QString text = ui->textEdit->toPlainText();

        RecvBox* recvbox = new RecvBox(ui->scrollArea);

        ui->scrollLayoutWidget->layout()->addWidget(recvbox); // 加入垂直布局中

        QPixmap p(":/eromanga.png");
        recvbox->setAvatar(p);
        recvbox->setBackgroundColor(Qt::white);
        recvbox->setBordetRadius(10);
        recvbox->setTextColor(Qt::black);

        if(!text.isEmpty())
            recvbox->setText(text);

        // 将滚动条值设置为最大值，以确保内容滚动到最底部
        ui->scrollArea->verticalScrollBar()->setValue(ui->scrollArea->verticalScrollBar()->maximum());
    });


    connect(ui->pushButton_2,&QPushButton::clicked,this,[=]{

        QString text = ui->textEdit->toPlainText();

        SendBox* sendbox = new SendBox(ui->scrollArea);

        ui->scrollLayoutWidget->layout()->addWidget(sendbox);

        QPixmap p(":/sky.jpg");
        sendbox->setAvatar(p);
        sendbox->setBackgroundColor(QColor(0,153,255));
        sendbox->setBordetRadius(10);
        sendbox->setTextColor(Qt::white);

        if(!text.isEmpty())
            sendbox->setText(text);

        ui->scrollArea->verticalScrollBar()->setValue(ui->scrollArea->verticalScrollBar()->maximum());
    });


}

Widget::~Widget()
{
    delete ui;
}

